package com.epmis.epmis.Service.impl;

import com.epmis.epmis.Mapper.MissionMapper;
import com.epmis.epmis.Mapper.ProjectMapper;
import com.epmis.epmis.Mapper.UserMapper;
import com.epmis.epmis.Service.ProjectService;
import com.epmis.epmis.pojo.*;
import com.epmis.epmis.utils.ImageUitl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Sets;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ProjectServiceImpl implements ProjectService {
    @Resource
    ProjectMapper projectMapper;
    @Resource
    MissionMapper missionMapper;
    @Resource
    UserMapper userMapper;

    @Override
    public PageInfo<HashMap<String,Object>> getAllProjectTable(int pageCode, int pageSize, String id,int enterpriseId) {
        HashMap<String,Object> getProject = new HashMap();
        getProject.put("id",id);
        getProject.put("enterpriseId",enterpriseId);
        PageHelper.startPage(pageCode,pageSize);
        List<Project> projects = projectMapper.getAllProject(getProject);
        PageInfo pageInfo = new PageInfo<>(projects);
        List<Project> project = pageInfo.getList();
        List<HashMap<String,Object>> list = project.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            newMap.put("projectName",res.getProject_name());
            newMap.put("id",res.getId());
            if(res.getPrincipal_id() == 0){
                newMap.put("principalName","无");
            }else{
                User user = projectMapper.getHeadImgAndName(res.getPrincipal_id());
                newMap.put("principalName",user.getName());
                try {
                    newMap.put("circleUrl","data:image/jpeg;base64,"+ ImageUitl.ImageToBase64ByLocal(user.getPhoto()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            int All = projectMapper.getProjectAllMission(Integer.valueOf(id)).size();
            int complete = projectMapper.getProjectCompleteMission(Integer.valueOf(id)).size();
            int process = 0;
            if(All != 0){
                process = complete/All;
            }
            newMap.put("process",process);
            newMap.put("status",res.getState());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            newMap.put("createDate",sdf.format(res.getCreate_date()));
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getPublicProject(int pageCode, int pageSize, String id,int enterpriseId) {
        PageHelper.startPage(pageCode,pageSize);
        List<Project> projects = projectMapper.getPublicProject(enterpriseId);
        PageInfo pageInfo = new PageInfo<>(projects);
        List<Project> project = pageInfo.getList();
        List<HashMap<String,Object>> list = project.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            newMap.put("projectName",res.getProject_name());
            newMap.put("id",res.getId());
            if(res.getPrincipal_id() == 0){
                newMap.put("principalName","无");
            }else{
                User user = projectMapper.getHeadImgAndName(res.getPrincipal_id());
                newMap.put("principalName",user.getName());
                try {
                    newMap.put("circleUrl","data:image/jpeg;base64,"+ ImageUitl.ImageToBase64ByLocal(user.getPhoto()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            int All = projectMapper.getProjectAllMission(Integer.valueOf(id)).size();
            int complete = projectMapper.getProjectCompleteMission(Integer.valueOf(id)).size();
            int process = 0;
            if(All != 0){
                process = complete/All;
            }
            newMap.put("process",process);
            newMap.put("status",res.getState());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            newMap.put("createDate",sdf.format(res.getCreate_date()));
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getPrivateProject(int pageCode, int pageSize, String id) {
        PageHelper.startPage(pageCode,pageSize);
        List<Project> projects = projectMapper.getPrivateProject(id);
        PageInfo pageInfo = new PageInfo<>(projects);
        List<Project> project = pageInfo.getList();
        List<HashMap<String,Object>> list = project.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            newMap.put("projectName",res.getProject_name());
            newMap.put("id",res.getId());
            if(res.getPrincipal_id() == 0){
                newMap.put("principalName","无");
            }else{
                User user = projectMapper.getHeadImgAndName(res.getPrincipal_id());
                newMap.put("principalName",user.getName());
                try {
                    newMap.put("circleUrl","data:image/jpeg;base64,"+ ImageUitl.ImageToBase64ByLocal(user.getPhoto()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            int All = projectMapper.getProjectAllMission(Integer.valueOf(id)).size();
            int complete = projectMapper.getProjectCompleteMission(Integer.valueOf(id)).size();
            int process = 0;
            if(All != 0){
                process = complete/All;
            }
            newMap.put("process",process);
            newMap.put("status",res.getState());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            newMap.put("createDate",sdf.format(res.getCreate_date()));
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> findAllProject(int pageCode, int pageSize, String id,String content,int enterpriseId)  {
        HashMap<String,Object> getProject = new HashMap();
        getProject.put("id",id);
        getProject.put("content",content);
        getProject.put("enterpriseId",enterpriseId);
        PageHelper.startPage(pageCode,pageSize);
        List<Project> projects = projectMapper.findAllProject(getProject);
        PageInfo pageInfo = new PageInfo<>(projects);
        List<Project> project = pageInfo.getList();
        List<HashMap<String,Object>> list = project.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            newMap.put("projectName",res.getProject_name());
            newMap.put("id",res.getId());
            if(res.getPrincipal_id() == 0){
                newMap.put("principalName","无");
            }else{
                User user = projectMapper.getHeadImgAndName(res.getPrincipal_id());
                newMap.put("principalName",user.getName());
                try {
                    newMap.put("circleUrl","data:image/jpeg;base64,"+ ImageUitl.ImageToBase64ByLocal(user.getPhoto()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            int All = projectMapper.getProjectAllMission(Integer.valueOf(id)).size();
            int complete = projectMapper.getProjectCompleteMission(Integer.valueOf(id)).size();
            int process = 0;
            if(All != 0){
                process = complete/All;
            }
            newMap.put("process",process);
            newMap.put("status",res.getState());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            newMap.put("createDate",sdf.format(res.getCreate_date()));
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> findAllDeleteProject(int pageCode, int pageSize, String id, String content, int enterpriseId) {
        HashMap<String,Object> getProject = new HashMap();
        getProject.put("id",id);
        getProject.put("enterpriseId",enterpriseId);
        getProject.put("content",content);
        PageHelper.startPage(pageCode,pageSize);
        List<Project> projects = projectMapper.findAllDeleteProject(getProject);
        PageInfo pageInfo = new PageInfo<>(projects);
        List<Project> project = pageInfo.getList();
        List<HashMap<String,Object>> list = project.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            newMap.put("projectName",res.getProject_name());
            newMap.put("number",res.getProject_number());
            newMap.put("id",res.getId());

            User creator = projectMapper.getHeadImgAndName(res.getCreator_id());
            User deleter= projectMapper.getHeadImgAndName(res.getDeleter_id());
            newMap.put("creatorName",creator.getName());
            newMap.put("deleterName",deleter.getName());
            try {
                newMap.put("creatorHeadImg","data:image/jpeg;base64,"+ ImageUitl.ImageToBase64ByLocal(creator.getPhoto()));
                newMap.put("deleterHeadImg","data:image/jpeg;base64,"+ ImageUitl.ImageToBase64ByLocal(deleter.getPhoto()));
            } catch (IOException e) {
                e.printStackTrace();
            }

            int All = projectMapper.getProjectAllMission(Integer.valueOf(id)).size();
            newMap.put("missionNumber",All);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            newMap.put("deleteDate",sdf.format(res.getDelete_date()));
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public int updateDeleteProject(int id) {
        return projectMapper.updateDeleteProject(id);
    }

//    @Override
//    public int deleteProject(int id) {
//        //找到删除项目的任务
//        List<Mission> missions = missionMapper.getDeleteMission(id);
//
//        //删除任务的评论、活动、附件
//        for(int i = 0;i < missions.size();i++){
//            int missionId = missions.get(i).getId();
//            missionMapper.deleteProjectAccessory(id);
//
//            missionMapper.deleteMissionAllComment(missionId);
//            missionMapper.deleteMissionAllActivity(missionId);
//
//            HashMap<String,Object> missionMap = new HashMap<>();
//            missionMap.put("updateContent","," + missionId + ",");
//            missionMap.put("id",String.valueOf(missionId));
//
//            missionMapper.updateDeleteMissionSon(missionId);
//            missionMapper.updateDeleteRelMission(missionMap);
//
//            MissionProject missionProject = missionMapper.getDeleteMissionProject(missions.get(i).getId());
//            missionMapper.deleteDeleteMissionProjectHour(missionProject.getMission_id());
//            missionMapper.deleteMissionProject(missionId);
//
//            missionMapper.deleteCost(missionId);
//
//            missionMapper.deleteTarget(missionId);
//
//            missionMapper.updateDeleteMissionBefore(missionMap);
//            missionMapper.updateDeleteMissionAfter(missionMap);
//        }
//
//        projectMapper.deleteProjectMission(id);
//        projectMapper.deleteProject(id);
//
//        return 0;
//    }

    @Override
    public int updateProjectName(String project_name,int id) {
        HashMap<String,Object> map= new HashMap<>();
        map.put("project_name",project_name);
        map.put("id",id);
        return projectMapper.updateProjectName(map);
    }

    @Override
    public int cancelTop(int id) {
        return projectMapper.cancelTop(id);
    }

    @Override
    public int ToTop(int id) {
        return projectMapper.ToTop(id);
    }

    @Override
    public int updateProjectNumber(String project_number, int id,String enterprise_id) {
        if(userMapper.isExistSameNumber(project_number,enterprise_id) != null) {
            return 2;
        }
        HashMap<String,Object> map= new HashMap<>();
        map.put("project_number",project_number);
        map.put("id",id);
        return projectMapper.updateProjectNumber(map);
    }

    @Override
    public int updateProjectRouge(int rouge, int id) {
        return projectMapper.updateProjectRouge(rouge,id);
    }

    @Override
    public int updateProjectDescription(String description, int id) {
        HashMap<String,Object> map= new HashMap<>();
        map.put("description",description);
        map.put("id",id);
        return projectMapper.updateProjectDescription(map);
    }

    @Override
    public int updateProjectState(String state, int id) {
        HashMap<String,Object> map= new HashMap<>();
        map.put("state",state);
        map.put("id",id);
        return projectMapper.updateProjectState(map);
    }

    @Override
    public int updateProjectStateDescription(String state_description, int id) {
        HashMap<String,Object> map= new HashMap<>();
        map.put("state_description",state_description);
        map.put("id",id);
        return projectMapper.updateProjectStateDescription(map);
    }

    @Override
    public int updateProjectCustomerUnit(String customer_unit, int id) {
        HashMap<String,Object> map= new HashMap<>();
        map.put("customer_unit",customer_unit);
        map.put("id",id);
        return projectMapper.updateProjectCustomerUnit(map);
    }

    @Override
    public int updateProjectCustomerRepresentative(String customer_representative, int id) {
        HashMap<String,Object> map= new HashMap<>();
        map.put("customer_representative",customer_representative);
        map.put("id",id);
        return projectMapper.updateProjectCustomerRepresentative(map);
    }

    @Override
    public int updateProjectType(String project_type, int id) {
        HashMap<String,Object> map= new HashMap<>();
        map.put("project_type",project_type);
        map.put("id",id);
        return projectMapper.updateProjectType(map);
    }

    @Override
    public int updateStartDate(Date start_date, int id) {
        HashMap<String,Object> map= new HashMap<>();
        map.put("start_date",start_date);
        map.put("id",id);
        return projectMapper.updateStartDate(map);
    }

    @Override
    public int updateOverDate(Date over_date, int id) {
        HashMap<String,Object> map= new HashMap<>();
        map.put("over_date",over_date);
        map.put("id",id);
        return projectMapper.updateOverDate(map);
    }

    @Override
    public int updateProjectPrincipal(int principal_id, int id) {
        return projectMapper.updateProjectPrincipal(principal_id,id);
    }

    @Override
    public int deleteProject1(int deleteId, int projectId) {
        HashMap<String,Object> map= new HashMap<>();
        SimpleDateFormat simpleDateFormat;
        simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        map.put("deleter_id",deleteId);
        map.put("delete_date",simpleDateFormat.format(date));
        map.put("id",projectId);
        projectMapper.updateDelete(projectId);
        projectMapper.updateDeleteMission(projectId);
        return projectMapper.updateProjectDelete(map);
    }

    @Override
    public List<HashMap<String, Object>> getProjectList(String id, int enterpriseId) {
        HashMap<String,Object> getProject = new HashMap();
        getProject.put("id",id);
        getProject.put("enterpriseId",enterpriseId);
        List<Project> projects = projectMapper.getAllProject(getProject);
        List<HashMap<String,Object>> list = projects.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            newMap.put("id",res.getId());
            newMap.put("name",res.getProject_name());
            return newMap;
        }).collect(Collectors.toList());
        return list;
    }

    @Override
    public List<HashMap<String,Object>> getTopProjectList(String id, int enterpriseId) {
        HashMap<String,Object> getProject = new HashMap();
        getProject.put("id",id);
        getProject.put("enterpriseId",enterpriseId);
        List<Project> projects = projectMapper.getAllTopProject(getProject);
        List<HashMap<String,Object>> list = projects.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            newMap.put("id",res.getId());
            newMap.put("name",res.getProject_name());
            return newMap;
        }).collect(Collectors.toList());
        return list;
    }

    @Override
    public HashMap<String,Object> getProjectMessage(int id) {
        HashMap<String,Object> ProjectMap = new HashMap<>();
        Project project = projectMapper.getProjectMessage(id);
        ProjectMap.put("projectName",project.getProject_name());
        ProjectMap.put("topStatus",project.getTop_state());
        ProjectMap.put("principalId",project.getPrincipal_id());
        ProjectMap.put("creatorId",project.getCreator_id());
        if(project.getPrincipal_id() == 0){
            ProjectMap.put("principalName","无");
            ProjectMap.put("principalHead","无");
        }else{
            User user = userMapper.getUserMessage(project.getPrincipal_id());
            ProjectMap.put("principalName",user.getName());
            try {
                ProjectMap.put("principalHead","data:image/jpeg;base64,"+ ImageUitl.ImageToBase64ByLocal(user.getPhoto()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(project.getStart_date() == null){
            ProjectMap.put("startDate","");
        }else {
            ProjectMap.put("startDate",sdf.format(project.getStart_date()));
        }
        if(project.getOver_date() == null){
            ProjectMap.put("overDate","");
        }else {
            ProjectMap.put("overDate",sdf.format(project.getOver_date()));
        }
        ProjectMap.put("currentStatus",project.getState());
        ProjectMap.put("rouge",project.getRouge());
        ProjectMap.put("ProjectNumber",project.getProject_number());
        String memberArray = "";
        String[] members = project.getMember_id().split(",");
            for(int i = 0; i < members.length ; i++) {
                if(i == members.length - 1){
                    memberArray = memberArray + members[i];
                }else if(i != 0){
                    memberArray = memberArray + members[i] + ",";
                }
            }
        ProjectMap.put("membersId",memberArray);
        ProjectMap.put("projectType",project.getProject_type());
        ProjectMap.put("unit",project.getCustomer_unit());
        ProjectMap.put("representative",project.getCustomer_representative());
        ProjectMap.put("StateDescription",project.getState_description());
        ProjectMap.put("description",project.getDescription());
        int all = projectMapper.getProjectAllMission(id).size();
        int ws = projectMapper.getProjectAllWSMission(id).size();
        int doing = projectMapper.getProjectAllDoingMission(id).size();
        int complete = projectMapper.getProjectCompleteMission(id).size();
        int postpone = projectMapper.getProjectAllPostponeMission(id).size();
        int completeRate;
        int postponeRate;
        if(all == 0){
            completeRate = 0;
            postponeRate = 0;
        }else {
            completeRate = complete / all;
            postponeRate = postpone / all;
        }

        ProjectMap.put("all",all);
        ProjectMap.put("ws",ws);
        ProjectMap.put("doing",doing);
        ProjectMap.put("complete",complete);
        ProjectMap.put("postpone",postpone);
        ProjectMap.put("completeRate",completeRate);
        ProjectMap.put("postponeRate",postponeRate);
        int missionProject = projectMapper.getProjectAllMissionProject(id).size();
        int target = projectMapper.getProjectAllTarget(id).size();
        int cost = projectMapper.getProjectAllCost(id).size();
        ProjectMap.put("missionProject",missionProject);
        ProjectMap.put("target",target);
        ProjectMap.put("cost",cost);

        return ProjectMap;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getSearchAllTarget(int pageCode, int pageSize, String id, String MissionId) {
        PageHelper.startPage(pageCode,pageSize);
        List<Target> targets = missionMapper.getSearchAllTarget(Integer.valueOf(id),Integer.valueOf(MissionId));
        PageInfo pageInfo = new PageInfo<>(targets);
        List<Target> target = pageInfo.getList();
        List<HashMap<String,Object>> list = target.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            newMap.put("status",res.getTarget_status());
            Mission mission = missionMapper.getMission(res.getMission_id());
            newMap.put("name",mission.getTitle());
            newMap.put("id",mission.getId());
            if(mission.getPrincipal_id() == 0){
                newMap.put("principalName","无");
            }else {
                newMap.put("principalId",mission.getPrincipal_id());
                User user = projectMapper.getHeadImgAndName(mission.getPrincipal_id());
                newMap.put("principalName",user.getName());
                try {
                    newMap.put("circleUrl","data:image/jpeg;base64,"+ ImageUitl.ImageToBase64ByLocal(user.getPhoto()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            newMap.put("targetValue",res.getTarget_value());
            newMap.put("currentValue",res.getCurrent_value());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(mission.getOver_date() == null) {
                newMap.put("overDate","");
            }else {
                newMap.put("overDate",sdf.format(mission.getOver_date()));
            }
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getAllTarget(int pageCode, int pageSize, String id) {
        PageHelper.startPage(pageCode,pageSize);
        List<Target> targets = missionMapper.getAllTarget(Integer.valueOf(id));
        PageInfo pageInfo = new PageInfo<>(targets);
        List<Target> target = pageInfo.getList();
        List<HashMap<String,Object>> list = target.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            newMap.put("status",res.getTarget_status());
            Mission mission = missionMapper.getMission(res.getMission_id());
            newMap.put("name",mission.getTitle());
            newMap.put("id",mission.getId());
            if(mission.getPrincipal_id() == 0){
                newMap.put("principalName","无");
            }else {
                newMap.put("principalId",mission.getPrincipal_id());
                User user = projectMapper.getHeadImgAndName(mission.getPrincipal_id());
                newMap.put("principalName",user.getName());
                try {
                    newMap.put("circleUrl","data:image/jpeg;base64,"+ ImageUitl.ImageToBase64ByLocal(user.getPhoto()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            newMap.put("targetValue",res.getTarget_value());
            newMap.put("currentValue",res.getCurrent_value());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(mission.getOver_date() == null) {
                newMap.put("overDate","");
            }else {
                newMap.put("overDate",sdf.format(mission.getOver_date()));
            }
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getDangerMission(int pageCode, int pageSize, String id) {
        PageHelper.startPage(pageCode,pageSize);
        List<Target> targets = missionMapper.getDangerMission(Integer.valueOf(id));
        PageInfo pageInfo = new PageInfo<>(targets);
        List<Target> target = pageInfo.getList();
        List<HashMap<String,Object>> list = target.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            newMap.put("status",res.getTarget_status());
            Mission mission = missionMapper.getMission(res.getMission_id());
            newMap.put("name",mission.getTitle());
            newMap.put("id",mission.getId());
            if(mission.getPrincipal_id() == 0){
                newMap.put("principalName","无");
            }else {
                newMap.put("principalId",mission.getPrincipal_id());
                User user = projectMapper.getHeadImgAndName(mission.getPrincipal_id());
                newMap.put("principalName",user.getName());
                try {
                    newMap.put("circleUrl","data:image/jpeg;base64,"+ ImageUitl.ImageToBase64ByLocal(user.getPhoto()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            newMap.put("targetValue",res.getTarget_value());
            newMap.put("currentValue",res.getCurrent_value());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(mission.getOver_date() == null) {
                newMap.put("overDate","");
            }else {
                newMap.put("overDate",sdf.format(mission.getOver_date()));
            }
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getCompleteMission(int pageCode, int pageSize, String id) {
        PageHelper.startPage(pageCode,pageSize);
        List<Target> targets = missionMapper.getCompleteMission(Integer.valueOf(id));
        PageInfo pageInfo = new PageInfo<>(targets);
        List<Target> target = pageInfo.getList();
        List<HashMap<String,Object>> list = target.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            newMap.put("status",res.getTarget_status());
            Mission mission = missionMapper.getMission(res.getMission_id());
            newMap.put("name",mission.getTitle());
            newMap.put("id",mission.getId());
            if(mission.getPrincipal_id() == 0){
                newMap.put("principalId","无");
            }else {
                newMap.put("principalId",mission.getPrincipal_id());
                User user = projectMapper.getHeadImgAndName(mission.getPrincipal_id());
                newMap.put("principalName",user.getName());
                try {
                    newMap.put("circleUrl","data:image/jpeg;base64,"+ ImageUitl.ImageToBase64ByLocal(user.getPhoto()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            newMap.put("targetValue",res.getTarget_value());
            newMap.put("currentValue",res.getCurrent_value());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(mission.getOver_date() == null) {
                newMap.put("overDate","");
            }else {
                newMap.put("overDate",sdf.format(mission.getOver_date()));
            }
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getAllProjectMission(int pageCode, int pageSize, String id) {
        PageHelper.startPage(pageCode,pageSize);
        List<MissionProject> missionProjects = missionMapper.getAllProjectMission(Integer.valueOf(id));
        PageInfo pageInfo = new PageInfo<>(missionProjects);
        List<MissionProject> MissionProject = pageInfo.getList();
        List<HashMap<String,Object>> list = MissionProject.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            newMap.put("status",res.getPM_state());
            Mission mission = missionMapper.getMission(res.getMission_id());
            newMap.put("name",mission.getTitle());
            newMap.put("id",mission.getId());
            newMap.put("principalId",mission.getPrincipal_id());
            User user = projectMapper.getHeadImgAndName(mission.getPrincipal_id());
            newMap.put("principalName",user.getName());
            try {
                newMap.put("circleUrl","data:image/jpeg;base64,"+ ImageUitl.ImageToBase64ByLocal(user.getPhoto()));
            } catch (IOException e) {
                e.printStackTrace();
            }
            newMap.put("priority",mission.getPriority());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            newMap.put("startDate",sdf.format(mission.getStart_date()));
            if(mission.getOver_date() == null) {
                newMap.put("overDate","");
            }else {
                newMap.put("overDate",sdf.format(mission.getOver_date()));
            }
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getSearchProjectMission(int pageCode, int pageSize, String id, String missionId) {
        PageHelper.startPage(pageCode,pageSize);
        List<MissionProject> missionProjects = missionMapper.getSearchProjectMission(Integer.valueOf(id),Integer.valueOf(missionId));
        PageInfo pageInfo = new PageInfo<>(missionProjects);
        List<MissionProject> MissionProject = pageInfo.getList();
        List<HashMap<String,Object>> list = MissionProject.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            newMap.put("status",res.getPM_state());
            Mission mission = missionMapper.getMission(res.getMission_id());
            newMap.put("name",mission.getTitle());
            newMap.put("id",mission.getId());
            newMap.put("principalId",mission.getPrincipal_id());
            User user = projectMapper.getHeadImgAndName(mission.getPrincipal_id());
            newMap.put("principalName",user.getName());
            try {
                newMap.put("circleUrl","data:image/jpeg;base64,"+ ImageUitl.ImageToBase64ByLocal(user.getPhoto()));
            } catch (IOException e) {
                e.printStackTrace();
            }
            newMap.put("priority",mission.getPriority());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            newMap.put("startDate",sdf.format(mission.getStart_date()));
            newMap.put("overDate",sdf.format(mission.getOver_date()));
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getMyProjectMission(int pageCode, int pageSize, String id, String principal_id) {
        PageHelper.startPage(pageCode,pageSize);
        List<MissionProject> missionProjects = missionMapper.getMyProjectMission(Integer.valueOf(id),Integer.valueOf(principal_id));
        PageInfo pageInfo = new PageInfo<>(missionProjects);
        List<MissionProject> MissionProject = pageInfo.getList();
        List<HashMap<String,Object>> list = MissionProject.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            newMap.put("status",res.getPM_state());
            Mission mission = missionMapper.getMission(res.getMission_id());
            newMap.put("name",mission.getTitle());
            newMap.put("id",mission.getId());
            newMap.put("principalId",mission.getPrincipal_id());
            User user = projectMapper.getHeadImgAndName(mission.getPrincipal_id());
            newMap.put("principalName",user.getName());
            try {
                newMap.put("circleUrl","data:image/jpeg;base64,"+ ImageUitl.ImageToBase64ByLocal(user.getPhoto()));
            } catch (IOException e) {
                e.printStackTrace();
            }
            newMap.put("priority",mission.getPriority());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            newMap.put("startDate",sdf.format(mission.getStart_date()));
            newMap.put("overDate",sdf.format(mission.getOver_date()));
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getDelayProjectMission(int pageCode, int pageSize, String id) {
        PageHelper.startPage(pageCode,pageSize);
        List<MissionProject> missionProjects = missionMapper.getDelayProjectMission(Integer.valueOf(id));
        PageInfo pageInfo = new PageInfo<>(missionProjects);
        List<MissionProject> MissionProject = pageInfo.getList();
        List<HashMap<String,Object>> list = MissionProject.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            newMap.put("status",res.getPM_state());
            Mission mission = missionMapper.getMission(res.getMission_id());
            newMap.put("name",mission.getTitle());
            newMap.put("id",mission.getId());
            newMap.put("principalId",mission.getPrincipal_id());
            User user = projectMapper.getHeadImgAndName(mission.getPrincipal_id());
            newMap.put("principalName",user.getName());
            try {
                newMap.put("circleUrl","data:image/jpeg;base64,"+ ImageUitl.ImageToBase64ByLocal(user.getPhoto()));
            } catch (IOException e) {
                e.printStackTrace();
            }
            newMap.put("priority",mission.getPriority());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            newMap.put("startDate",sdf.format(mission.getStart_date()));
            newMap.put("overDate",sdf.format(mission.getOver_date()));
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getAllCost(int pageCode, int pageSize, String id) {
        PageHelper.startPage(pageCode,pageSize);
        List<Cost> costs = missionMapper.getAllCost(Integer.valueOf(id));
        PageInfo pageInfo = new PageInfo<>(costs);
        List<Cost> cost = pageInfo.getList();
        List<HashMap<String,Object>> list = cost.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            newMap.put("status",res.getCost_state());
            Mission mission = missionMapper.getMission(res.getMission_id());
            newMap.put("name",mission.getTitle());
            newMap.put("id",mission.getId());
            newMap.put("finance",res.getFinance());
            newMap.put("budget",res.getBudget());
            newMap.put("execute",res.getExecute());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(res.getPayment_date() == null){
                newMap.put("payDate","");
            }else {
                newMap.put("payDate",sdf.format(res.getPayment_date()));
            }
            if(res.getHappen_date() == null){
                newMap.put("happenDate","");
            }else {
                newMap.put("happenDate",sdf.format(res.getHappen_date()));
            }

            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getSearchCost(int pageCode, int pageSize, String id, String missionId) {
        PageHelper.startPage(pageCode,pageSize);
        List<Cost> costs = missionMapper.getSearchCost(Integer.valueOf(id),Integer.valueOf(missionId));
        PageInfo pageInfo = new PageInfo<>(costs);
        List<Cost> cost = pageInfo.getList();
        List<HashMap<String,Object>> list = cost.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            newMap.put("status",res.getCost_state());
            Mission mission = missionMapper.getMission(res.getMission_id());
            newMap.put("name",mission.getTitle());
            newMap.put("id",mission.getId());
            newMap.put("finance",res.getFinance());
            newMap.put("budget",res.getBudget());
            newMap.put("execute",res.getExecute());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(res.getPayment_date() == null){
                newMap.put("payDate","");
            }else {
                newMap.put("payDate",sdf.format(res.getPayment_date()));
            }
            if(res.getHappen_date() == null){
                newMap.put("happenDate","");
            }else {
                newMap.put("happenDate",sdf.format(res.getHappen_date()));
            }
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getMyCost(int pageCode, int pageSize, String id, String principal_id) {
        PageHelper.startPage(pageCode,pageSize);
        List<Cost> costs = missionMapper.getMyCost(Integer.valueOf(id),Integer.valueOf(principal_id));
        PageInfo pageInfo = new PageInfo<>(costs);
        List<Cost> cost = pageInfo.getList();
        List<HashMap<String,Object>> list = cost.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            newMap.put("status",res.getCost_state());
            Mission mission = missionMapper.getMission(res.getMission_id());
            newMap.put("name",mission.getTitle());
            newMap.put("id",mission.getId());
            newMap.put("finance",res.getFinance());
            newMap.put("budget",res.getBudget());
            newMap.put("execute",res.getExecute());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(res.getPayment_date() == null){
                newMap.put("payDate","");
            }else {
                newMap.put("payDate",sdf.format(res.getPayment_date()));
            }
            if(res.getHappen_date()== null){
                newMap.put("happenDate","");
            }else {
                newMap.put("happenDate",sdf.format(res.getHappen_date()));
            }
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getBackCost(int pageCode, int pageSize, String id) {
        PageHelper.startPage(pageCode,pageSize);
        List<Cost> costs = missionMapper.getBackCost(Integer.valueOf(id));
        PageInfo pageInfo = new PageInfo<>(costs);
        List<Cost> cost = pageInfo.getList();
        List<HashMap<String,Object>> list = cost.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            newMap.put("status",res.getCost_state());
            Mission mission = missionMapper.getMission(res.getMission_id());
            newMap.put("name",mission.getTitle());
            newMap.put("id",mission.getId());
            newMap.put("finance",res.getFinance());
            newMap.put("budget",res.getBudget());
            newMap.put("execute",res.getExecute());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(res.getPayment_date() == null){
                newMap.put("payDate","");
            }else {
                newMap.put("payDate",sdf.format(res.getPayment_date()));
            }
            if(res.getHappen_date() == null){
                newMap.put("happenDate","");
            }else {
                newMap.put("happenDate",sdf.format(res.getHappen_date()));
            }
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getHappenCost(int pageCode, int pageSize, String id) {
        PageHelper.startPage(pageCode,pageSize);
        List<Cost> costs = missionMapper.getHappenCost(Integer.valueOf(id));
        PageInfo pageInfo = new PageInfo<>(costs);
        List<Cost> cost = pageInfo.getList();
        List<HashMap<String,Object>> list = cost.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            newMap.put("status",res.getCost_state());
            Mission mission = missionMapper.getMission(res.getMission_id());
            newMap.put("name",mission.getTitle());
            newMap.put("id",mission.getId());
            newMap.put("finance",res.getFinance());
            newMap.put("budget",res.getBudget());
            newMap.put("execute",res.getExecute());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(res.getPayment_date() == null){
                newMap.put("payDate","");
            }else {
                newMap.put("payDate",sdf.format(res.getPayment_date()));
            }
            if(res.getHappen_date() == null){
                newMap.put("happenDate","");
            }else {
                newMap.put("happenDate",sdf.format(res.getHappen_date()));
            }
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public int updateProjectMembers(List oldMembers, List newMembers,int projectId) {
        Set<Integer> set1 = new HashSet<Integer>(oldMembers);
        Set<Integer> set2 = new HashSet<Integer>(newMembers);
        //删除
        Sets.SetView<Integer> delete = Sets.difference(set1,set2);

        int exist = 0;
        if(delete.size() != 0){
            //删除人员
            HashMap<String,Object> map = new HashMap<>();
            map.put("id", Arrays.asList(delete.toArray()).get(0));
            map.put("project_id",projectId);
            exist = projectMapper.findIsExistMissionMember(map).size();
        }

        if(exist != 0){
            return 2;
        }else{
            String projectsMembers = "";
            for(int i = 0;i < newMembers.size();i++){
                if(i == 0){
                    projectsMembers = "," + projectsMembers + newMembers.get(i) + ",";
                }else{
                    projectsMembers = projectsMembers + newMembers.get(i) + ",";
                }
            }
            HashMap<String,Object> map = new HashMap<>();
            map.put("member_id",projectsMembers);
            map.put("id",projectId);
            return projectMapper.updateProjectMembers(map);
        }
    }
}
